exports.propagationList = (req, res) => {

    var restfulParams = req.routeInfo.restfulParams;

    var resultData = {};

    var todayStart = new Date(new Date().toLocaleDateString()).getTime() / 1000;

    var todayEnd = (new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000) / 1000;

    var u_id = restfulParams.u_id;

    var u_is_manager = restfulParams.u_is_manager;

    var u_type = restfulParams.u_type;

    var conditionToday = "/apurr_uid/" + u_id + "/between/" + todayStart + "-apurr_create_time-" + todayEnd;

    var conditionTotal = "/apurr_uid/" + u_id;

    if (u_type < 1 && u_is_manager > 0) {

        conditionToday = "/between/" + todayStart + "-apurr_create_time-" + todayEnd;

        conditionTotal = ""
    }

    //查询媒体传播信息
    function propagationInfo(cb) {

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.artMediaCommunicationSearch.Request_Api_Url + "/mc_is_delete/0",
                method: 'POST',
                data: {
                    fields: ['mc_id', 'mc_url', 'mc_title_image', 'mc_title', 'mc_create_time', 'mc_apid'].join(",")
                }
            },
            function (data) {

                resultData.propagationInfo = data.result.data;

                cb();

            }
        )
    }

    //查询每个传播媒体的相关数据
    function queryForEachMediaData(cb) {

        F.SerialEach(resultData.propagationInfo, function (item, index, callback) {

            //查询阅读，分享，成交数
            function propagationRelationCount(eachCb) {

                R.SEND_HTTP(
                    req,
                    res,
                    {
                        url: config.external_server.apiAddress + "/organization/search_art_product_user_read_record/apuur_mcid/" + item.mc_id,
                        method: 'POST',
                        data: {
                            fields: ['apurr_id', 'apurr_read_count', 'apurr_share_count', 'apurr_status',].join(",")
                        }
                    },
                    function (data) {

                        var propagationData = data.result.data;

                        var browseCount = 0;
                        var shareCount = 0;
                        var buyCount = 0;
                        if (propagationData.length > 0) {
                            for (var i = 0; i < propagationData.length; i++) {
                                browseCount += parseInt(propagationData[i].apurr_read_count);
                                shareCount += parseInt(propagationData[i].apurr_share_count);
                                if (propagationData[i].apurr_status == 1) {
                                    buyCount++
                                }
                            }
                        }

                        item.browseCount = browseCount;
                        item.shareCount = shareCount;
                        item.buyCount = buyCount;

                        eachCb();

                    }
                )
            }

            F.Parallel([
                propagationRelationCount,
            ], () => {
                callback();
            })

        }, cb)

    }

    //今日阅读人数
    function CalculateTodayReading(cb) {

        R.SEND_HTTP(
            req,
            res,
            {
                url: config.external_server.apiAddress + "/organization/search_art_product_user_read_record" + conditionToday,
                method: 'POST',
                data: {
                    fields: ['apurr_id', 'apuur_mcid',].join(",")
                }
            },
            function (data) {

                var todayReading = [];

                if (data.result.data.length > 0) {
                    for (var i = 0; i < data.result.data.length; i++) {
                        if (data.result.data[i].apuur_mcid > 0) {
                            todayReading.push(data.result.data[i])
                        }
                    }
                }

                resultData.todayBrowseCount = todayReading.length;

                cb();

            }
        )
    }

    // 总阅读人数
    function CalculateTotalReading(cb) {
        R.SEND_HTTP(
            req,
            res,
            {
                url: config.external_server.apiAddress + "/organization/search_art_product_user_read_record" + conditionTotal,
                method: 'POST',
                data: {
                    fields: ['apurr_id', 'apuur_mcid',].join(",")
                }
            },
            function (data) {

                var todayReading = [];

                if (data.result.data.length > 0) {
                    for (var i = 0; i < data.result.data.length; i++) {
                        if (data.result.data[i].apuur_mcid > 0) {
                            todayReading.push(data.result.data[i])
                        }
                    }
                }

                resultData.totalBrowseCount = todayReading.length;

                cb();

            }
        )
    }

    F.Serial([
        propagationInfo,
        queryForEachMediaData,
        CalculateTodayReading,
        CalculateTotalReading,
    ], () => {
        RES.successResponse(res, resultData);
    })


}